﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using CAMS.Models;

namespace CAMS.Controllers
{
    public class aaModelsController : Controller
    {
        private readonly CAMSContext _context;

        public aaModelsController(CAMSContext context)
        {
            _context = context;
        }

        // GET: aaModels
        public async Task<IActionResult> Index()
        {
            return View(await _context.aaModel.ToListAsync());
        }

        public async Task<IActionResult> a()
        {
            List<aaModel> obj = new List<aaModel>();
            obj.Add(new aaModel() { UserName = "23" });
            obj.Add(new aaModel() { UserName = "19",ConfirmPassword="enen!" });
            obj.Add(new aaModel() { UserName = "12", Password="123" });
            return View("Index",obj);
        }

        // GET: aaModels/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var aaModel = await _context.aaModel
                .FirstOrDefaultAsync(m => m.ID == id);
            if (aaModel == null)
            {
                return NotFound();
            }

            return View(aaModel);
        }

        // GET: aaModels/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: aaModels/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("ID,UserName,Password,ConfirmPassword")] aaModel aaModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(aaModel);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(aaModel);
        }

        // GET: aaModels/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var aaModel = await _context.aaModel.FindAsync(id);
            if (aaModel == null)
            {
                return NotFound();
            }
            return View(aaModel);
        }

        // POST: aaModels/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("ID,UserName,Password,ConfirmPassword")] aaModel aaModel)
        {
            if (id != aaModel.ID)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(aaModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!aaModelExists(aaModel.ID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(aaModel);
        }

        // GET: aaModels/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var aaModel = await _context.aaModel
                .FirstOrDefaultAsync(m => m.ID == id);
            if (aaModel == null)
            {
                return NotFound();
            }

            return View(aaModel);
        }

        // POST: aaModels/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var aaModel = await _context.aaModel.FindAsync(id);
            _context.aaModel.Remove(aaModel);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool aaModelExists(int id)
        {
            return _context.aaModel.Any(e => e.ID == id);
        }
    }
}
